package team.szm.scd.common;

import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import team.szm.scd.controller.UserControllerTests;
import team.szm.scd.database.dto.Result;
import team.szm.scd.database.dto.UserLogin;

public class LoginTestUtils {
    public static void testNeedLogin(ResultActions r) throws Exception {
        r.andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(Result.Code.NEED_LOGIN))
                .andExpect(MockMvcResultMatchers.jsonPath("$.message")
                        .value(Result.Message.NEED_LOGIN));
    }

    public static void login(MockMvc mockMvc, MockHttpSession session) throws Exception {
        String verify_code = VerifyCodeUtils.getVerifyCode(mockMvc, session);
        UserLogin login = UserControllerTests.getTestLogin(verify_code);
        ResultActions resultActions = UserControllerTests.loginWithMockMvc(login, mockMvc, session);
        UserControllerTests.testSuccess(resultActions);
    }
}
